#include "previewer.h"
#include "ui_previewer.h"

#include <QMdiSubWindow>
#include <QTimer>
#include <QMenu>

Previewer::Previewer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Previewer)
{
    ui->setupUi(this);

    // Disable tab #4 to preview disabled tabs
    ui->tabWidget->setTabEnabled(3, false);

    // menu for toolbutton
    QMenu *toolButtonMenu = new QMenu(this);
    toolButtonMenu->addAction("Item1");
    toolButtonMenu->addSeparator();
    toolButtonMenu->addAction("Item2");
    toolButtonMenu->addSeparator();
    QAction *act = toolButtonMenu->addAction("Item3");
    act->setEnabled(false);
    ui->toolButton->setMenu(toolButtonMenu);

    QTimer *timerProgress = new QTimer(this);
    timerProgress->setInterval(500);
    connect(timerProgress, SIGNAL(timeout()), this, SLOT(slotProgress()));
    timerProgress->start();

    // some MDI windows
    QMdiSubWindow *mdi = ui->mdiArea->addSubWindow(new QLabel("MDI", ui->mdiArea));
    mdi->resize(160, 80);

#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
    ui->tree->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
#else
    ui->tree->header()->setResizeMode(QHeaderView::ResizeToContents);
    ui->table->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
#endif

    ui->tree->topLevelItem(0)->setExpanded(true);
}

Previewer::~Previewer()
{
    delete ui;
}

int Previewer::addTab(QWidget *page, const QString &label)
{
    return ui->tabWidget->addTab(page, label);
}

void Previewer::slotApplyCss(const QString &style)
{
    ui->widgetAllWidgets->setStyleSheet(style);
    ui->toolButton->menu()->setStyleSheet(ui->widgetAllWidgets->styleSheet());
}

void Previewer::slotProgress()
{
    int value = ui->progressBar->value();

    if(value == ui->progressBar->maximum())
        value = 0;
    else
        value++;

    ui->progressBar->setValue(value);
}
